package com.yn.bsc.web.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yn.bsc.core.Result;
import com.yn.bsc.core.ResultGenerator;
import com.yn.bsc.core.Service;

/**
 * 通用Controller（增删改查）
 * 
 * @ClassName BaseController
 * @author z
 * @date 2017年9月20日 上午10:36:00
 */
public abstract class BaseController<S extends Service<T>, T> {

	@Autowired
	protected S service;

	@PostMapping("/add")
	public Result add(T t) {
		service.save(t);
		return ResultGenerator.genSuccessResult();
	}

	@PostMapping("/delete")
	public Result delete(@RequestParam Integer id) {
		service.deleteById(id);
		return ResultGenerator.genSuccessResult();
	}

	@PostMapping("/update")
	public Result update(T t) {
		service.update(t);
		return ResultGenerator.genSuccessResult();
	}

	@PostMapping("/detail")
	public Result detail(@RequestParam Integer id) {
		T t = service.findById(id);
		return ResultGenerator.genSuccessResult(t);
	}

	@SuppressWarnings("rawtypes")
	@PostMapping("/list")
	public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
		PageHelper.startPage(page, size);
		List<T> list = service.findAll();
		@SuppressWarnings("unchecked")
		PageInfo pageInfo = new PageInfo(list);
		return ResultGenerator.genSuccessResult(pageInfo);
	}
}
